package com.itcast.controller;

import com.itcast.annotation.Log;
import com.itcast.pojo.PageBean;
import com.itcast.pojo.Result;
import com.itcast.pojo.Student;
import com.itcast.pojo.StudentQueryParam;
import com.itcast.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学员列表数据的条件分页查询
     * @param param
     * @return
     */
    @GetMapping
    public Result listQuery(StudentQueryParam param){
        log.info("学员列表数据的条件分页查询{}",param);
        PageBean pageBean = studentService.listQuery(param);
        return Result.success(pageBean);
    }

    /**
     * 批量删除学员信息
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除学员id为{}的信息",ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加学员信息
     * @param student
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加学员信息{}",student);
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据主键ID查询学员的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据主键ID查询学员的信息，{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员的数据信息
     * @param student
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学员的数据信息,{}",student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 修改学员的违纪信息
     * @param id
     * @param score
     * @return
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violationUpdate(@PathVariable Integer id, @PathVariable Integer score){
        log.info("修改学员的违纪信息,id{},score{}",id,score);
        studentService.violationUpdate(id,score);
        return Result.success();
    }
}
